home *** CD-ROM | disk | FTP | other *** search
- /*
- *--- Convert.cpp -------------------------------------------------------
- * Copyright (c) 1992-96 Adobe Systems, Inc. All rights reserved.
- *
- * PageMaker plug-in.
- *-----------------------------------------------------------------------
- */
-
- /* ---------------- PageMaker SDK include files ---------------- */
- #include "PMPlugin.h"
-
- /* ---------------- Plug-in include files ---------------- */
- #include "Convert.h"
-
- /* ---------------- Cross-platform include files ---------------- */
- #include <string.h>
- #include <stdio.h>
-
- /* ---------------- Platform specific include files ---------------- */
- #ifdef MACINTOSH
- #ifndef powerc
- #include <A4Stuff.h>
- #include <SetUpA4.h>
- #endif
- #include <Packages.h>
- #else
- #include <Windows.h>
- #endif
-
- /* ---------------- Type definitions ---------------- */
-
- /* ---------------- Definitions ---------------- */
-
- /* ---------------- PageMaker (New)SDK include files ---------------- */
- #include "CICommandsAndQueries.h"
-
- /* ---------------- Function declarations ---------------- */
- void XltIntlDecimal(char * buffer);
- char DecPtChar(void);
- PMXErr SizeToTwips(float srcSize, long *Twips);
- PMXErr TwipsToSize(long Twips, float *destSize);
- PMXErr SizeToString(long srcSize, char *destStr, size_t *destStrLen);
-
-
- /* ---------------- Globals ---------------- */
- kUnits gSrcType, gDestType;
- PMBool gLocalise;
- extern sPMParamBlockPtr thePB;
-
- PMXErr Convert(kUnits srcType, float srcSize, kUnits destType, char *destStr,
- float *destSize, size_t *destStrLen, PMBool localise)
- {
- PMXErr result = 1;
- long lTwips;
- float dSize;
- size_t sDestStrLen;
-
- #if __MWERKS__ && __MC68K__
- long oldA4 = SetCurrentA4();
- RememberA4();
- #endif
-
- gLocalise = localise;
-
- gSrcType = srcType; gDestType = destType;
-
- result = SizeToTwips(srcSize, &lTwips);
- if (result != 0)
- goto onErr;
-
- result = TwipsToSize(lTwips, &dSize);
- if (result != 0)
- goto onErr;
-
- *destSize = dSize;
-
- result = SizeToString(lTwips, destStr, &sDestStrLen);
- if (result != 0)
- goto onErr;
-
- *destStrLen = sDestStrLen;
-
- #if __MWERKS__ && __MC68K__
- SetA4(oldA4);
- #endif
-
- onErr:
- return result;
- }
-
- PMXErr TwipsToSize(long Twips, float *destSize)
- {
- PMXErr result = 0;
- float divisor;
-
- switch (gDestType) {
- case kInches:
- divisor = kTwipsPerInch/10;
- break;
-
- case kInchesDecimal:
- divisor = kTwipsPerInch;
- break;
-
- case kMillimeters:
- divisor = kTwipsPerMillimeter;
- break;
-
- case kPicas:
- divisor = kTwipsPerPica;
- break;
-
- case kCiceros:
- divisor = kTwipsPerCicero;
- break;
-
- case kTwips:
- divisor = 1;
- break;
-
- default:
- result = -1; // unit is not handled. We do not handle dontcare as well.
- break;
- }
-
- *destSize = (float)(Twips / divisor);
-
- return result;
- }
-
- PMXErr SizeToTwips(float srcSize, long *Twips)
- {
- PMXErr result = 0;
- float divisor;
-
- switch (gSrcType) {
- case kInches:
- divisor = kTwipsPerInch/10;
- break;
-
- case kInchesDecimal:
- divisor = kTwipsPerInch;
- break;
-
- case kMillimeters:
- divisor = kTwipsPerMillimeter;
- break;
-
- case kPicas:
- divisor = kTwipsPerPica;
- break;
-
- case kCiceros:
- divisor = kTwipsPerCicero;
- break;
-
- case kTwips:
- divisor = 1;
- break;
-
- default:
- result = -1;
- break;
- }
-
- *Twips = (long)(srcSize * divisor);
-
- return result;
- }
-
- PMXErr SizeToString(long srcSize, char *destStr, size_t *destStrLen)
- {
- PMXErr result = 0;
- struct {
- long dSize;
- short dUnit;
- } convValue;
-
- convValue.dSize = srcSize;
- convValue.dUnit = gDestType;
-
- memset(destStr, 0, 256);
-
- result = PBBinQueryWithParms( thePB, pm_getconverttwips, kXRSPointer, &convValue,
- sizeof(convValue), kXRSPointer, destStr, 256 );
- if (result != CQ_SUCCESS)
- goto onErr;
- /*
- short x;
- long tempLong;
- char tempString[128], smallStr[128];
-
- destStr[0] = 0;
-
- if ( srcSize < 0 ) {
- strcat( destStr, "-" );
- srcSize *= -1;
- }
-
- tempLong = srcSize;
- sprintf( tempString, "%ld", tempLong );
- strcat( destStr, tempString );
-
- tempString[0] = 0;
- for ( x = 0; x < precision; x++ ) {
- srcSize *= 10;
- tempLong *= 10;
- srcSize -= tempLong;
- if ( srcSize == 0 ) break;
- tempLong = srcSize;
- sprintf( smallStr, "%ld", tempLong );
- strcat( tempString, smallStr );
- }
-
- if ( strlen(tempString) ) {
- strcat( destStr, "." );
- strcat( destStr, tempString );
- }
- */
-
- if (gLocalise)
- XltIntlDecimal(destStr);
-
- *destStrLen = strlen(destStr);
-
- onErr:
- return result;
- }
-
- void XltIntlDecimal(char * buffer)
- {
-
- static char decimalPt = 0;
-
- if (!decimalPt)
- decimalPt = DecPtChar();
-
-
- if ( decimalPt != '.' ) {
- if (buffer = strchr(buffer, '.'))
- *buffer = decimalPt;
- }
- }
-
-
- char DecPtChar(void)
- {
- #ifdef MACINTOSH
- char decPt;
-
- Intl0Hndl iHndl;
- iHndl = (Intl0Hndl)IUGetIntl(0);
- if (iHndl)
- decPt = (**iHndl).decimalPt;
- else
- decPt = '.'; // default value
-
- return decPt;
- #endif
- #ifdef WINDOWS
- char decPt[2];
-
- GetProfileString("intl", "sDecimal", ".", decPt, sizeof(decPt));
-
- return decPt[0];
- #endif
- }